// Il s'agit du fichier DLL principal.

#include "Core.h"
#include "SudokuCore.h"
#include "SudokuRules.h"

#include <iostream>

JNIEXPORT jshortArray JNICALL Java_sudoku_core_Core_generate
  (JNIEnv * env, jclass, jint level)
{
	short grid[81] = {0};

	SudokuCore::generate(grid);
	SudokuRules rules(grid);
	rules.generate(level);

	jshortArray jgrid = env->NewShortArray(81);
	env->SetShortArrayRegion(jgrid, 0, 81, (jshort*) rules.grid);

	return jgrid;
}

JNIEXPORT jboolean JNICALL Java_sudoku_core_Core_solve
  (JNIEnv * env, jclass, jshortArray jgrid)
{
	jshort* pGrid = env->GetShortArrayElements(jgrid, (jboolean) false);
	short grid[81] = {0};
	for (short i = 0; i < 81; i++)
	{
		grid[i] = *pGrid;
		pGrid++;
	}
	unsigned int s = SudokuCore::solve(grid, 1);

	if (s > 0)
	{
		env->SetShortArrayRegion(jgrid, 0, 81, (jshort*) grid);
	}

	return (jboolean) (s > 0);
}

JNIEXPORT jboolean JNICALL Java_sudoku_core_Core_validate
  (JNIEnv * env, jclass, jshortArray jgrid)
{
	jshort* pGrid = env->GetShortArrayElements(jgrid, (jboolean) false);
	short grid[81] = {0};
	for (short i = 0; i < 81; i++)
	{
		grid[i] = *pGrid;
		pGrid++;
	}
	unsigned int s = SudokuCore::solve(grid, 2);

	return (jboolean) (s == 1);
}
